// 时间日期工具类

// 封装一个不够两位数就补零的函数
function showTime(t) {
  return t > 9 ? t : `0${t}`
}

function now() {
  return new Date()
}

function toDateStr(date) {
  if (!date) {
    return date
  }
  if (typeof date === 'string') {
    return date
  }
  if (typeof date === 'number') {
    date = new Date(date)
  }
  const year = date.getFullYear()// 年
  const month = showTime(date.getMonth() + 1)// 月
  const day = showTime(date.getDate())// 日
  return `${year}-${month}-${day}`
}

function toDateTimeStr(date) {
  if (!date) {
    return date
  }
  if (typeof date === 'string') {
    return date
  }
  const year = date.getFullYear()// 年
  const month = showTime(date.getMonth() + 1)// 月
  const day = showTime(date.getDate())// 日
  const hours = showTime(date.getHours())// 小时
  const minutes = showTime(date.getMinutes())// 分钟
  const second = showTime(date.getSeconds())// 秒
  return `${year}-${month}-${day} ${hours}:${minutes}:${second}`
}

function nowStr() {
  return toDateTimeStr(now())
}

function thisDayStr() {
  return toDateStr(now())
}

// 本周第一天
function thisWeekFirstDate() {
  const date = new Date()
  return new Date(date.getTime() - (3600 * 1000 * 24 * (date.getDay() === 0 ? 6 : date.getDay() - 1)))
}

function thisWeekFirst() {
  return toDateStr(thisWeekFirstDate())
}

// 本周最后一天
function thisWeekEndDate() {
  const date = new Date()
  return new Date(date.getTime() + (3600 * 1000 * 24 * (date.getDay() === 0 ? 0 : 7 - date.getDay())))
}

function thisWeekEnd() {
  return toDateStr(thisWeekEndDate())
}

function thisWeekDate() {
  return [thisWeekFirstDate(), thisWeekEndDate()]
}

function thisWeek() {
  return [thisWeekFirst(), thisWeekEnd()]
}

// 本月第一天
function thisMonthFirstDate() {
  const date = new Date()
  date.setDate(1)
  return date
}

function thisMonthFirst() {
  return toDateStr(thisMonthFirstDate())
}

// 本月最后一天
function thisMonthEndDate() {
  const date = new Date()
  return new Date(date.getFullYear(), date.getMonth() + 1, 0)
}

function thisMonthEnd() {
  return toDateStr(thisMonthEndDate())
}

function thisMonthDate() {
  return [thisMonthFirstDate(), thisMonthEndDate()]
}

function thisMonth() {
  return [thisMonthFirst(), thisMonthEnd()]
}

// 本季度第一天
function thisQuarterFirstDate() {
  const date = new Date()
  const month = date.getMonth()
  const quarter = Math.floor(month / 3) + 1
  const startMonth = (quarter * 3) - 2
  date.setMonth(startMonth - 1)
  date.setDate(1)
  return date
}

function thisQuarterFirst() {
  return toDateStr(thisQuarterFirstDate())
}

// 本季度最后一天
function thisQuarterEndDate() {
  const date = new Date()
  const month = date.getMonth()
  const quarter = Math.floor(month / 3) + 1
  const lastMonth = quarter * 3
  return new Date(date.getFullYear(), lastMonth, 0)
}

function thisQuarterEnd() {
  return toDateStr(thisQuarterEndDate())
}

function thisQuarterDate() {
  return [thisQuarterFirstDate(), thisQuarterEndDate()]
}

function thisQuarter() {
  return [thisQuarterFirst(), thisQuarterEnd()]
}

// 本年第一天
function thisYearFirstDate() {
  const date = new Date()
  date.setDate(1)
  date.setMonth(0)
  return date
}

function thisYearFirst() {
  return toDateStr(thisYearFirstDate())
}

// 本年最后一天
function thisYearEndDate() {
  const date = new Date()
  date.setFullYear(date.getFullYear() + 1)
  date.setDate(0)
  date.setMonth(-1)
  return date
}

function thisYearEnd() {
  return toDateStr(thisYearEndDate())
}

function thisYearDate() {
  return [thisYearFirstDate(), thisYearEndDate()]
}

function thisYear() {
  return [thisYearFirst(), thisYearEnd()]
}

// 日期快捷选项 - 适配 Element Plus
const DatePickerOptions = {
  shortcuts: [
    {
      text: '今天',
      value: new Date()
    },
    {
      text: '昨天',
      value: () => {
        const date = new Date()
        date.setTime(date.getTime() - 3600 * 1000 * 24)
        return date
      }
    },
    {
      text: '一周前',
      value: () => {
        const date = new Date()
        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
        return date
      }
    },
    {
      text: '一月前',
      value: () => {
        const date = new Date()
        date.setTime(date.getTime() - 3600 * 1000 * 24 * 30)
        return date
      }
    },
    {
      text: '一年前',
      value: () => {
        const date = new Date()
        date.setTime(date.getTime() - 3600 * 1000 * 24 * 365)
        return date
      }
    }
  ]
}

// 日期范围快捷选项 - 适配 Element Plus
const DateRangePickerOptions = {
  shortcuts: [
    {
      text: '今天',
      value: () => {
        const end = new Date()
        const start = new Date()
        return [start, end]
      }
    },
    {
      text: '最近一周',
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        return [start, end]
      }
    },
    {
      text: '最近一个月',
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        return [start, end]
      }
    },
    {
      text: '最近三个月',
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        return [start, end]
      }
    },
    {
      text: '本周',
      value: () => {
        return [thisWeekFirstDate(), thisWeekEndDate()]
      }
    },
    {
      text: '本月',
      value: () => {
        return [thisMonthFirstDate(), thisMonthEndDate()]
      }
    },
    {
      text: '本季度',
      value: () => {
        return [thisQuarterFirstDate(), thisQuarterEndDate()]
      }
    },
    {
      text: '本年',
      value: () => {
        return [thisYearFirstDate(), thisYearEndDate()]
      }
    }
  ]
}

export default {
  now,
  nowStr,
  thisDayStr,
  toDateStr,
  toDateTimeStr,
  thisWeekFirstDate,
  thisWeekFirst,
  thisWeekEndDate,
  thisWeekEnd,
  thisWeekDate,
  thisWeek,
  thisMonthFirstDate,
  thisMonthFirst,
  thisMonthEndDate,
  thisMonthEnd,
  thisMonthDate,
  thisMonth,
  thisQuarterFirstDate,
  thisQuarterFirst,
  thisQuarterEndDate,
  thisQuarterEnd,
  thisQuarterDate,
  thisQuarter,
  thisYearFirstDate,
  thisYearFirst,
  thisYearEndDate,
  thisYearEnd,
  thisYearDate,
  thisYear,
  DatePickerOptions,
  DateRangePickerOptions
}
